/**
 *  *2010-5-30 上午12:45:56  add 
 */
package com.base.myproject.client.HR.bcode;

import java.util.Date;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.CommandSyncContainer;
import com.base.myproject.client.tools.CommandSyncExeSql;
import com.base.myproject.client.tools.CommandSyncGetModelDatas;
import com.base.myproject.client.tools.CommandSyncReturnObject;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.base.myproject.client.ui.dialog.SelectItemDialog;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.fx.FxConfig;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.ListView;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;

/**
 * 宿舍管理
 * 
 * @author Administrator 2010-5-30 javazgw@gmail.com
 */
public class HR_hostel extends BaseJDBCPanel {
	ListView<BusiObject> view = null;
	ListView<BusiObject> bcodeview = null;

	Button addhoster = new Button("添加宿舍");
	Button delhoster = new Button("删除宿舍");
	Button save = new Button("保存");
	Button addbcode = new Button("添加人员");
	Button delbcode = new Button("删除人员");
	Button showLeavePeople = new Button("离宿人员");
	
	String code = "";
	Label L1=new Label("编号:");
	TextField<String> seach_code  = new TextField<String>(); 
	Label L2=new Label("名称:");
	TextField<String> seach_text  = new TextField<String>(); //查询框
	TextField<String> bednum_text  = new TextField<String>();
	BaseComboBoxX sex = new BaseComboBoxX("name", "dictionary","where type='hr_sex'");
	Button seach = new Button("宿舍查询");
	CheckBox isOk = new CheckBox();
	CheckBox noBody = new CheckBox();
	
	public HR_hostel() {
		super();
		init();
	}

	public void init() {
		this.add(initWeigetx());
		addhoster.addListener(Events.Select, getListener());
		delhoster.addListener(Events.Select, getListener());
		save.addListener(Events.Select, getListener());
		addbcode.addListener(Events.Select, getListener());
		delbcode.addListener(Events.Select, getListener());
		showLeavePeople.addListener(Events.Select, getListener());
		registerMainStore(store);
		registerStore(store);
	}
	
	public ContentPanel initWeigetx() {

		ContentPanel panel = new ContentPanel();
		panel.setHeaderVisible(false);
		panel.setBodyBorder(false);
		HorizontalPanel h2 = new HorizontalPanel();
		h2.add(addhoster);
		h2.add(delhoster);
		h2.add(save);
		VerticalPanel vv = new VerticalPanel();
		vv.add(CreateView());
		vv.add(createForm());
		vv.add(h2);
		
		HorizontalPanel h1 = new HorizontalPanel();
		HorizontalPanel h = new HorizontalPanel();
		h.setSpacing(3);
		h1.add(addbcode);
		h1.add(delbcode);
		
		VerticalPanel v = new VerticalPanel();
		v.setSpacing(5);
		HorizontalPanel sea = new HorizontalPanel();
		HorizontalPanel s = new HorizontalPanel();
		HorizontalPanel s1 = new HorizontalPanel();
		seach_text.setWidth(60);
		seach_code.setWidth(60);
		sea.add(L1);
		sea.add(seach_code);
		seach_code.setWidth(100);
		sea.add(L2);
		sea.add(seach_text);
		seach_text.setWidth(100);
		
		s1.add(new Label("性别:"));
		s1.add(sex);
		sex.setWidth(100);
		s.add(new Label("床位未满:"));
		isOk.setWidth(60);
		s.add(isOk);
		
		s.add(new Label("未住人:"));
		noBody.setWidth(60);
		s.add(noBody);
		s1.add(seach);
		s1.add(showLeavePeople);
		v.add(sea);
		v.add(s);
		v.add(s1);
		seach.addListener(Events.Select, getListener());
		
		v.add(CreateBcodeView());
		v.add(createbcodeForm());
		v.add(h1);
	
		h.add(vv);
		h.add(v);
		panel.add(h);
		
		return panel;
	}

	BaseStore<BusiObject> store = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("code");
		type.getField("code").setType(String.class);
		type.addField("name");
		type.getField("name").setType(String.class);
		type.addField("addr");
		type.getField("addr").setType(String.class);
		type.addField("sex");
		type.getField("sex").setType(String.class);
		type.addField("mark");
		type.getField("mark").setType(String.class);
		type.addField("bednum");
		type.getField("bednum").setType(Integer.class);
		type.addField("type");
		type.getField("type").setType(String.class);
		type.addField("imagepath");
		type.getField("imagepath").setType(String.class);
		type.addField("usednum");
		type.getField("usednum").setType(Integer.class);

		StoreInfo info = new StoreInfo();
		info.setTablename("hr_Hostel");
		info.setKeyname(new String[] { "icode" });
		info.setNeedAllRow("false");
		info.setForcenolimit("true");
		info.setSql("select icode,hr_Hostel.code,hr_Hostel.bednum, hr_Hostel.addr,hr_Hostel.name,hr_Hostel.sex,hr_Hostel.mark,"+
				"hr_Hostel.type,'images/b2cimages/hoster.gif' as imagepath, (select count(*) " +
				"from hr_Hostel_bcode where code = hr_Hostel.code and (leavedate is null or convert(varchar(10), leavedate, 120) >= convert(varchar(10), getDate(), 120))) as usednum from hr_Hostel order by hr_Hostel.code asc");
		store = new BaseStore<BusiObject>(type, info);
		return store;
	}

	BaseStore<BusiObject> hr_hostel_bcodestore = null;

	BaseStore<BusiObject> gethr_hostel_bcodeStore() {
		if (hr_hostel_bcodestore == null) {
			createhr_hostel_bcodeStore();
		}
		return hr_hostel_bcodestore;
	}

	BaseStore<BusiObject> createhr_hostel_bcodeStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("code");
		type.getField("code").setType(String.class);
		type.addField("imagepath");
		type.getField("imagepath").setType(String.class);
		type.addField("livedate");
		type.getField("livedate").setType(Date.class);
		type.addField("leavedate");
		type.getField("leavedate").setType(Date.class);
		type.addField("bednum");
		type.getField("bednum").setType(String.class);

		StoreInfo info = new StoreInfo();
		info.setTablename("hr_hostel_bcode");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select icode,bcode,livedate,code,(select top 1 bcodeimagepath from bcode_images where bcode_images.bcode=hr_hostel_bcode.bcode)  as imagepath,bednum,leavedate from hr_hostel_bcode where code='{code}' and (leavedate is null or convert(varchar(10), leavedate, 120) >= convert(varchar(10), getdate(), 120)) ");
		info.setParent(store);// 关联主表
		info.setLinkparentcolumns(new String[] { "code" }); // 关联主表字段
		info.setLinkparent_chile_columns(new String[] { "code" });// 关联主表字段
		hr_hostel_bcodestore = new BaseStore<BusiObject>(type, info);
		return hr_hostel_bcodestore;
	}
	



	ContentPanel CreateView() {
		final ContentPanel panel = new ContentPanel();
		panel.setHeaderVisible(false);
		panel.setFrame(true);
		panel.setId("images-view");
		panel.setHeight(374);
		panel.setBodyBorder(false);

		view = new ListView<BusiObject>() {
			@Override
			protected BusiObject prepareData(BusiObject model) {
				return model;
			}
		};

		view.setTemplate(getTemplate());
		view.setStore(createStore());
		view.setHeight(354);
		view.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
		view.setItemSelector("div.thumb-wrap");
		view.getSelectionModel().addListener(Events.SelectionChange,
				new Listener<SelectionChangedEvent<BusiObject>>() {

					public void handleEvent(SelectionChangedEvent<BusiObject> be) {
						System.out.println("code===" + view.getSelectionModel()
						.getSelectedItem().get("code"));
						code = view.getSelectionModel()
						.getSelectedItem().get("code");
						
						if (store.indexOf(view.getSelectionModel()
								.getSelectedItem()) >= 0)
							store.gotorow(store.indexOf(view
									.getSelectionModel().getSelectedItem()));
					}
				});

		panel.add(view);
		return panel;
	}

	ContentPanel CreateBcodeView() {
		final ContentPanel panel = new ContentPanel();
		panel.setFrame(true);
		panel.setId("images-view");
		panel.setHeaderVisible(false);
		panel.setHeight(220);
		panel.setWidth(320);
		panel.setBodyBorder(false);

		bcodeview = new ListView<BusiObject>() {
			@Override
			protected BusiObject prepareData(BusiObject model) {
				return model;
			}
		};
		bcodeview.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
		bcodeview.setTemplate(getBcodeTemplate());
		bcodeview.setStore(gethr_hostel_bcodeStore());
		bcodeview.setHeight(205);
		bcodeview.setWidth(320);
		bcodeview.setItemSelector("div.thumb-wrap");
		bcodeview.getSelectionModel().addListener(Events.SelectionChange,
				new Listener<SelectionChangedEvent<BusiObject>>() {

					public void handleEvent(SelectionChangedEvent<BusiObject> be) {
						if (hr_hostel_bcodestore.indexOf(bcodeview.getSelectionModel()
								.getSelectedItem()) >= 0)
							hr_hostel_bcodestore.gotorow(hr_hostel_bcodestore.indexOf(bcodeview
									.getSelectionModel().getSelectedItem()));

					}

				});
		panel.add(bcodeview);
		return panel;
	}

	BaseFormPanel createbcodeForm() {
		BaseFormPanel form = new BaseFormPanel(gethr_hostel_bcodeStore(),false);
		form.setHeaderVisible(false);
		TableLayout tl = new TableLayout(2);

		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);
		form.setLayout(tl);
		form.setHeaderVisible(false);
		form.setPadding(5);
		form.add(new Label("姓名:"), td);
		BaseComboBoxForm<BaseModelData> bcode = new BaseComboBoxForm<BaseModelData>("bcode","bname","v_bcode2job","",true);
		bcode.setName("bcode");
		bcode.setReadOnly(true);
		bcode.setAllowBlank(false);
		form.add(bcode, td);
		
		
		form.add(new Label("所属部门:"), td);
		BaseComboBoxForm<BaseModelData> dept = new BaseComboBoxForm<BaseModelData>("bcode","departments","bcode","",true);
		dept.setName("bcode");
		dept.setReadOnly(true);
		dept.setAllowBlank(false);
		form.add(dept, td);
		
		form.add(new Label("入住日期:"), td);
		DateField datef = new DateField(); 
		datef.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));  
		datef.setName("livedate");
		form.add(datef, td);
		form.add(new Label("床位："),td);
		bednum_text.setName("bednum");
		form.add(bednum_text,td);
		form.add(new Label("退宿日期"),td);
		DateField df = new DateField();
		df.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		df.setName("leavedate");
		form.add(df,td);
		
		form.inited();
		return form;
	};
	/*****************/
	TextField<String> C_num2;
	TextField<String> C_num3;
	TextField<String> C_num4;
	BaseComboBoxX C_num5;
	NumberField C_num7;
	BaseComboBoxX C_num8;
	BaseFormPanel createForm() {
		BaseFormPanel form = new BaseFormPanel(getStore(),false);
		form.setHeaderVisible(false);
		TableLayout tl = new TableLayout(8);

		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);
		form.setLayout(tl);
		form.setHeaderVisible(false);
		form.setPadding(5);
	
		form.add(new Label("编号:"), td);
		C_num2 = new TextField<String>();
		C_num2.setWidth(127);
		C_num2.setName("code");
		C_num2.setAllowBlank(false);
		form.add(C_num2, td);
		form.add(new Label("名称:"), td);
		C_num3 = new TextField<String>();
		C_num3.setWidth(127);
		C_num3.setName("name");
		form.add(C_num3, td);
		form.add(new Label("地址:"), td);
		C_num4 = new TextField<String>();
		C_num4.setName("addr");
		C_num4.setWidth(127);
		form.add(C_num4, td);
		form.add(new Label("性别:"), td);
		C_num5 = new BaseComboBoxX("name", "dictionary","where type='hr_sex'");
		C_num5.setWidth(127);
		C_num5.setName("sex");
		form.add(C_num5, td);
		form.add(new Label("备注:"), td);
		TextField<String> C_num6 = new TextField<String>();
		C_num6.setName("mark");
		C_num6.setWidth(127);
		form.add(C_num6, td);
		form.add(new Label("床位:"), td);
		C_num7 = new NumberField();
		C_num7.setPropertyEditorType(Integer.class);
		C_num7.setName("bednum");
		C_num7.setWidth(127);
		form.add(C_num7, td);
		form.add(new Label("类型:"), td);
		C_num8 = new BaseComboBoxX("name", "dictionary", "where type='zy'");
		C_num8.setWidth(127);
		C_num8.setName("type");
		form.add(C_num8, td);
		form.inited();

		C_num2.addListener(Events.Change, getListener());
		C_num3.addListener(Events.Change, getListener());
		C_num4.addListener(Events.Change, getListener());
		C_num5.addListener(Events.Change, getListener());
		C_num7.addListener(Events.Change, getListener());
		C_num8.addListener(Events.Change, getListener());
		return form;
	};

	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {
							if (be.getSource().equals(addhoster)) {
								createDialog().show();
							} else if (be.getSource().equals(showLeavePeople)) {
								if (code.equals("")) {
									MessageBox.alert("错误", "请选择左边的宿舍，然后再" +
											"查询", null);
									return;
								}
								
								String oldSql = hr_hostel_bcodestore.getStoreinfo().getSql();
								String sql = "select icode,bcode,livedate,code,(select top 1 bcodeimagepath from bcode_images where bcode_images.bcode=hr_hostel_bcode.bcode)  as imagepath,bednum,leavedate from hr_hostel_bcode where code='" + code + "' and leavedate is not null";
								hr_hostel_bcodestore.getStoreinfo().setSql(sql);
								hr_hostel_bcodestore.reload();
								hr_hostel_bcodestore.getStoreinfo().setSql(oldSql);
							} else if (be.getSource().equals(delhoster)) {
								if (store.getCurcowindex() > 0) {
									store.remove(store.getAt(store
											.getCurcowindex()));
									store.trySave();
								}
								view.refresh();

							} else if (be.getSource().equals(addbcode)) {

								if (store.getCurcowindex() < 0) {
									MessageBox.alert("错误", "请先选择宿舍", null);
									return;
								}
								SelectItemDialog s = new SelectItemDialog() {
									protected void onOK() {
										final BusiObject bo = this.getGrid().getSelectionModel().getSelectedItem();
										if (bo != null) {
											String code = store.getAt(store.getCurcowindex()).get("code");
											String sql = "select (select case when records < Bednum then 1 else 0 end)as status1," +
													"(select case when d.sex = c.sex then 1 else 0 end)as status2 " +
													"from (select Bednum from hr_Hostel where code='"+code+"') as a," +
													"(select count(*) as records from hr_hostel_bcode where code='"+code+"') as b," +
													"(select sex from bcode where bcode='"+bo.get("bcode")+"') as c," +
													"(select sex from hr_Hostel where code='"+code+"') as d";
											final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT.create(com.base.myproject.client.tools.GreetingService.class);
											greetingService2.getDataSet(sql, new AsyncCallback<DataSet>(){
												@Override
												public void onFailure(Throwable caught) {	
												}
												@Override
												 public void onSuccess(DataSet result) {
													if(result.getRowCount() > 0){
														String res1 = result.getValue(0, "status1");
														String res2 = result.getValue(0, "status2");
														if(Integer.parseInt(res1) != 1){
															MessageBox.alert("错误", "宿舍床位已满", null);
														}else if(Integer.parseInt(res2) == 0){
															MessageBox.alert("错误", "所选人员与本宿舍性别不一致！", null);
														}else{
															BusiObject bcode_bo = new BusiObject();
															bcode_bo.set("bcode", bo.get("bcode"));
															bcode_bo.set("code", store.getValue("code"));
															
															java.util.Date currentTime = new java.util.Date();
															bcode_bo.set("livedate", DateTimeFormat.getFormat("yyyy-MM-dd").format(currentTime)); 

															hr_hostel_bcodestore.add(bcode_bo);
															hr_hostel_bcodestore.trySave();
															bcodeview.refresh();
														}
													}
												}
											});	   			
										}
										this.close();
									}
								};
								s.setColumndisplayname(new String[] { "编码","名称", "性别" });
								s.setColumnname(new String[] { "bcode","bname", "sex" });
								CommandSyncGetModelDatas csgmd2 = new CommandSyncGetModelDatas();
								
								csgmd2.setSqlname("hr_goods_hostel");
								s.setCsgmd2(csgmd2);
								s.show();
							} else if (be.getSource().equals(delbcode)) {

								if (bcodeview.getSelectionModel()
										.getSelectedItem() == null) {
									MessageBox.alert("错误", "请先选择人员", null);
									return;
								}
								// if(hr_hostel_bcodestore.getCurcowindex()>0)
								{
									hr_hostel_bcodestore.remove(bcodeview
											.getSelectionModel()
											.getSelectedItem());
									hr_hostel_bcodestore.trySave();
								}
								bcodeview.refresh();

							} else if (be.getSource().equals(save)) {
								store.trySave();
								view.refresh();
							}
							
							else if(be.getSource().equals(seach)){
								String code=seach_code.getRawValue();
								String name=seach_text.getRawValue();
								String sexName = sex.getRawValue();
								boolean c = isOk.getValue();
								boolean no = noBody.getValue();
								
								String where=" where 1=1";
								if(!code.equals("")){
									where+=" and code='"+code+"' ";
									System.out.println("sql--------------------" +"select icode,hr_Hostel.code,hr_Hostel.bednum, hr_Hostel.addr,hr_Hostel.name,hr_Hostel.sex,hr_Hostel.mark,"+
								"hr_Hostel.type,'images/b2cimages/hoster.gif' as imagepath, (select count(*) " +
								"from hr_Hostel_bcode where code = hr_Hostel.code and (leavedate is null or convert(varchar(10), leavedate, 120) >= convert(varchar(10), getDate(), 120))) as usednum from hr_Hostel"  + where);
								}
								if(!name.equals("")){
									where+=" and name='"+name+"' ";
								}
								if (!sexName.equals("")) {
									where+=" and sex='"+sexName+"' ";
								}
          			
								String sql = "select icode,hr_Hostel.code,hr_Hostel.bednum, hr_Hostel.addr,hr_Hostel.name,hr_Hostel.sex,hr_Hostel.mark,"+
								"hr_Hostel.type,'images/b2cimages/hoster.gif' as imagepath, (select count(*) " +
								"from hr_Hostel_bcode where code = hr_Hostel.code and (leavedate is null or convert(varchar(10), leavedate, 120) >= convert(varchar(10), getDate(), 120))) as usednum from hr_Hostel" + where ;
								if (c) {
									sql += " and bednum > (select count(*) from hr_Hostel_bcode where code = hr_Hostel.code) ";
							}
								if (no) {
									sql += " and (select count(*) from hr_Hostel_bcode where code = hr_Hostel.code) = 0 ";
								}

								StoreInfo info = new StoreInfo();
								info.setTablename("hr_Hostel");
								info.setKeyname(new String[] { "icode"});
								info.setSql(sql + " order by code asc");
								System.out.println("sql=========" + sql );
								store.setStoreinfo(info);
								store.reload();
								
							}
						} else if (be.getType() == Events.Change) {
							if (be.getSource().equals(C_num2)|| be.getSource().equals(C_num3)|| be.getSource().equals(C_num4)
									|| be.getSource().equals(C_num5)|| be.getSource().equals(C_num7)|| be.getSource().equals(C_num8)) {
								view.refresh();
							}
						} else if (be.getType() == Events.KeyUp) {
							if (be instanceof ComponentEvent) {
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{
								}
							}
						}
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}

	Dialog createDialog()
	{
		final Dialog d = new Dialog();
		final FormPanel fp = new FormPanel();
		final TextField<String> code = new TextField<String>();
		final NumberField bednum = new NumberField();
		code.setAllowBlank(false);
		bednum.setAllowBlank(false);
	
		code.setFieldLabel("宿舍号");
		bednum.setFieldLabel("床位");
		fp.add(code);
		fp.add(bednum);
		d.add(fp);
		d.setButtons(Dialog.OKCANCEL);
		d.getButtonById(Dialog.OK).addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				if(fp.isValid())
				{
					BusiObject bo = new BusiObject();
					bo.set("code",	code.getValue());
					bo.set("bednum", bednum.getValue().intValue());
					getStore().add(bo);
					view.getSelectionModel().select(store.getAt(store.getCurcowindex()), false);
					
					//添加钥匙
					CommandSyncContainer list = new CommandSyncContainer();
					
					for(int i=0;i< bednum.getValue().intValue();i++){
						CommandSyncExeSql csgmd = new CommandSyncExeSql();
						csgmd.setSqlname("hr_hostel_addkey");
						csgmd.putmacro("CODE", "宿舍钥匙"+code.getValue()+"-"+i);
						csgmd.putmacro("NAME", "宿舍钥匙"+code.getValue()+"-"+i);
					
						list.add(csgmd);
					}
					final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
							.create(com.base.myproject.client.tools.GreetingService.class);
					greetingService
							.SendCommandSync(
									list,
									new AsyncCallback<CommandSyncReturnObject>() {

										@Override
										public void onFailure(
												Throwable caught) {
											MessageBox.alert("错误",
													"网络连接失败", null);

										}

										@Override
										public void onSuccess(
												CommandSyncReturnObject result) {
											if (result.isB()) {
												Info.display("", "成功");
												d.hide();
											} else {
												MessageBox
														.alert(
																"错误",
																""
																		+ result
																				.getMessage(),
																null);
												d.el().blink(FxConfig.NONE);  
											}

										}

									});
				}
				
			}
			
		});
		d.getButtonById(Dialog.CANCEL).addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				d.setVisible(false);
			}
			
		});
		
		fp.setAutoHeight(true);
		fp.setAutoWidth(true);
		fp.setHeading("添加");
		d.setSize(366, 172);
		return d;
		
		
	}
	private native String getBcodeTemplate() /*-{
		return ['<tpl  for=".">', 
		'<div class="thumb-wrap" id="{bcode}" style="border: 1px solid white;float:left;">', 
		'<div class="thumb"  ><img style="width:60px;height:60px;border:5px double #eee;margin: 3px 2px 2px 3px" src="{imagepath}" title="{bcode}"></div>', 
		'<span class="x-editable" style="float:left" >{bcode} </span></div>', 
		'</tpl>',
		'<div class="x-clear"></div>'].join("");
	}-*/;

	private native String getTemplate() /*-{
		return ['<tpl  for=".">', 
		'<div class="thumb-wrap" id="{name}" style="border: 1px solid white;float:left;">', 
		'<div class="thumb"  ><img style="width:100px;height:100px;border:5px double #eee;margin: 3px 2px 2px 3px" src="{imagepath}" title="{name}"></div>', 
		'<span class="x-editable" style="float:left;height:17px" >{code} :{usednum}/{bednum} {type}</span></div>', 
		'</tpl>',
		'<div class="x-clear"></div>'].join("");
	}-*/;
}
